package com.onstar.cn.vehicle.util;

import android.content.Context;

import com.onstar.cn.common.log.LogManager;
import com.onstar.cn.common.util.CommonUtil;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 网络请求工具类     日期 2014-9-24
 */
public class RequstUtil {
	private static final String TAG = "RequstUtil";
	private static CommonUtil commonUtil;

	/**
	 * 网络可用状态下，通过get方式向server端发送请求，并返回响应数
	 * @param strUrl  请求网址
	 * @param context
	 * @return 响应数据
	 */
	public static JSONObject getResponseForGet(String strUrl, Context context) {
		commonUtil = new CommonUtil();
		if (CommonUtil.checkNetWorkState(context)) {
			return getResponseForGet(strUrl);
		}
		return null;
	}

	/**
	 * 通过Get方式处理请求，并返回相应数据
	 * @param strUrl  请求网址
	 * @return 响应数据
	 */
	public static JSONObject getResponseForGet(String strUrl) {
		HttpGet httpRequest = new HttpGet(strUrl);
		return getRespose(httpRequest);
	}

	/**
	 * * 网络可用状态下，通过post方式向server端发送请求，并返回响应数据*
	 *
	 * @param market_uri 请求网址
	 * @param nameValuePairs 参数信息
	 * @param context
	 * @return 响应数据
	 */
	public static JSONObject getResponseForPost(String market_uri, List<MroBasicNameValuePair> nameValuePairs, Context context) {
		commonUtil = new CommonUtil();
		if (CommonUtil.checkNetWorkState(context)) {
			return getResponseForPost(market_uri, nameValuePairs);
		}
		return null;
	}

	/**
	 * * 通过post方式向服务器发送请求，并返回响应数据
	 * @param market_uri   请求网址
	 * @param nameValuePairs  参数信息
	 * @return 响应数据
	 */
	public static JSONObject getResponseForPost(String market_uri, List<MroBasicNameValuePair> nameValuePairs) {
		if (null == market_uri || "" == market_uri) {
			return null;
		}
		HttpPost request = new HttpPost(market_uri);
		try {
			List<NameValuePair> pairs = new ArrayList<>();
			for(MroBasicNameValuePair pair : nameValuePairs){
				BasicNameValuePair basicNameValuePair = new BasicNameValuePair(pair.getName(),pair.getValue());
				pairs.add(basicNameValuePair);
			}
			request.setEntity(new UrlEncodedFormEntity(pairs, HTTP.UTF_8));
			return getRespose(request);
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		return null;
	}

	/**
	 * * 响应客户端请求*
	 * @param request  客户端请求get/post
	 * @return 响应数据
	 * */
	public static JSONObject getRespose(HttpUriRequest request) {
		try {
			// 链接超时，请求超时设??
			BasicHttpParams httpParams = new BasicHttpParams();
			HttpConnectionParams.setConnectionTimeout(httpParams, 10 * 1000);
			HttpConnectionParams.setSoTimeout(httpParams, 10 * 1000);
			HttpResponse httpResponse = new DefaultHttpClient(httpParams).execute(request);
			int statusCode = httpResponse.getStatusLine().getStatusCode();
			LogManager.getInstance().e(statusCode + "");
			if (HttpStatus.SC_OK == statusCode) {
				String result = EntityUtils.toString(httpResponse.getEntity());
				// Log.i(TAG, "results=" + result);
				return new JSONObject(result);
			} else {
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}
}
